/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import com.google.common.collect.Maps;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.ICountersResult;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

import java.util.Map;

/**
 * ClientUser object for the 'p4 counters' command.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class Counters extends AbstractPerforceCommand implements IPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the result of the operation. */
  private Result result = new Result();

  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Convenience method for creating instances of Counters.
   * 
   * @return the Counters object.
   */
  public static final Counters newInstance() {
    MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
    return new Counters(status);
  }

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Constructor for Counters.
   * 
   * @param status the MultiStatus.
   */
  private Counters(MultiStatus status) {
    super(status);
  }

  // -------------------------------------------------------- Public Methods
  // (IClientUserDelegate)

  @Override
  public void outputStat(Map<String, ?> strDict) {
    String key = (String) strDict.get("counter"); //$NON-NLS-1$
    Integer value = Helper.safeGetInteger(strDict, "value"); //$NON-NLS-1$

    result.putCounterValue(key, value);
  }

  // ----------------------------------------------------------- Public Methods
  // (IPerforceCommand)

  public String getCommand() {
    return "counters"; //$NON-NLS-1$
  }

  @Override
  public IStatus getStatus() {
    return getMultiStatus();
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Returns the result of the operation.
   * 
   * @return the result object.
   */
  public ICountersResult getResult() {
    return result;
  }

  // ----------------------------------------------------------------------
  // Private Nested Classes

  /**
   * Implementation of the ICountersResult. Stores information provided by the
   * 'p4 counters' command.
   * 
   * @see ICountersResult
   */
  private static class Result implements ICountersResult {
    // -----------------------------------------------------------------------
    // Private Variables

    /** Stores the mapping of counter name to its value. */
    private Map<String, Integer> counterValueMap = Maps.newHashMap();

    //--------------------------------------------------------------------------
    // Public Methods

    public Integer getCounterValue(String counterName) {
      return counterValueMap.get(counterName);
    }

    //--------------------------------------------------------------------------
    // ------- Methods

    /**
     * Call this method to associated the given counter name with the specified
     * value.
     * 
     * @param counterName the name of the counter.
     * @param value the value.
     */
    void putCounterValue(String counterName, Integer value) {
      counterValueMap.put(counterName, value);
    }
  }
}
